<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    public $helpers = array('Html', 'Form');
    public $allSubModulesDetails = array();
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'users',
                'action' => 'mydashboard'
            ),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'logout'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish',
                    'fields' => array('username' => 'email'),
                    'scope' => array('User.is_delete' => 0)
                )
            )
        ),
        'Cookie',
        'Paginator'
    );

    function beforeRender() {
        parent::beforeRender();

        //This array is used for grade array used in staff
        $STAFFGRADEARRAY = array();
        $STAFFGRADEARRAY['OPERATION'] = __('OPERATION', true);
        $STAFFGRADEARRAY['SYSTEM ADMINISTRATOR'] = __('SYSTEMADMINISTRATION', true);
        $STAFFGRADEARRAY['PARTNER'] = __('PARTNER', true);
        $STAFFGRADEARRAY['IN-CHARGE'] = __('INCHARGE', true);

        //This array is used for post array used in staff
        $STAFFPOSTARRAY = array();
        $STAFFPOSTARRAY['Secretary'] = __('SECRETARY', true);
        $STAFFPOSTARRAY['Office Manager'] = __('OFFICE_MANAGER', true);
        $STAFFPOSTARRAY['Partner'] = __('PARTNER', true);
        $STAFFPOSTARRAY['SA'] = __('SA', true);
        $STAFFPOSTARRAY['Assistant Solicitor'] = __('ASSISTANT_ SOLICITOR', true);
        $STAFFPOSTARRAY['Conveyancing Clerk'] = __('CONVEYANCING_CLERK', true);

        $this->set(compact(array('STAFFGRADEARRAY', 'STAFFPOSTARRAY')));

        $this->set(compact(array('STAFFGRADEARRAY', 'STAFFPOSTARRAY', 'DOCUMENTTYPEARRAY')));
        $this->set('actionTab', $this->params['action']);
        $this->set('controllerTab', $this->params['controller']);
        
        
        
    }

    //Pages that do not require Admin authentication
    function beforeFilter() {
        if ($this->Session->check('Config.language')) {
            Configure::write('Config.language', $this->Session->read('Config.language'));
        }
        
        $this->proceedToLogin();
        
        /*if (preg_match("/Trident\/7.0;(.*)rv:11.0/", $_SERVER["HTTP_USER_AGENT"], $match) != 0) {
            if($this->request->here != '/pages/browser_error'){
                return $this->redirect(array('controller' => 'pages', 'action' => 'browser_error'));
            }
        }*/
        if(isset($_GET['limit'])){
            
            $this->paginate = array('limit' => intval($_GET['limit']));
            if (!$this->request->is('ajax')) {
                if(isset($this->params->named['page'])){
                    $variablesDiff = "";
                    if(is_array($this->params->pass) && count($this->params->pass) > 0){
                        
                        foreach($this->params->pass as $valParams){
                            $variablesDiff.="/".$valParams;
                        }
                    }
                    $this->redirect(array('controller' => $this->params->controller,'action' => $this->params->action.$variablesDiff."?limit=".$_GET['limit']));
                }
            }
            
        } else{
            $this->paginate = array('limit' => 25);
        }

        parent::beforeFilter();
        $this->set('ALLOWEDSPECIALCHARACTER', $this->ALLOWEDSPECIALCHARACTER);
        $admin = Configure::read('Routing.prefixes');

        $this->Auth->allow('display', 'about_us', 'term_and_conditions');
        $this->Auth->autoRedirect = false;

        /*         * ***This section is for checking
          permissions those are defined in system
          modules and system_module_permissions.If
          user gets logged in then the permission
          check is processed******* */

        $systemUrl = $this->params['controller'] . "/" . $this->params['action'];

        //Please enter the functions in this array that are not ajax function and do not require the permission//
        $allowedModuleFunction = array();
        $allowedModuleFunction[] = array(
            "SystemModules/updateMultipleModulePermission"
        );
        
        if (($this->params['controller'] != "users") && (!in_array($systemUrl, $allowedModuleFunction))) {// as users function will be static function and do not required permissions
            if ($this->Auth->User('id') != "") {
                if (!($this->request->is('ajax'))) {
                    
                    if (($systemUrl != 'errors/errors') && ($systemUrl != 'pages/display')) {
                        $this->checkSystemAccess("/" . $systemUrl);
                    }
                }//if
            }
        }

        /*         * ************************************** */

        $msgFailed = __("LOGIN_FAILED", true);
        
        //$lang = $this->Cookie->read('lang');
        
        $lang = Configure::read('Config.language');

        $this->set('user_id', $this->Auth->User('id'));

        if ($lang == 'eng') {
            $this->set('user_first_name', $this->Auth->User('e_name'));
        } else {
            $this->set('user_first_name', $this->Auth->User('c_name'));
        }

        $this->set('usertype_id', $this->Auth->User('user_type'));

        /*         * ***** Code added to show the left navigation************ */
        $roleId = $this->Auth->User('role_id');
        
        App::Import("Model", "Role");
        $this->Role = new Role();
        $roles = $this->Role->find('list', array( 
                                    'fields' => array('Role.id', 'Role.role_name'))
                                );
        
        $this->set('roles', $roles);
        

        App::Import("Model", "SystemModule");
        $this->SystemModule = new SystemModule();
        $allModulesDetails = $this->SystemModule->find("all", array('fields' => array('SystemModule.id, SystemModule.module, SystemModule.module_chinese, SystemModule.module_url, SystemModule.module_unique_name, SystemModule.group, SystemModule.order'), 'conditions' => array('SystemModule.module_type' => '1'), 'order' => array('SystemModule.order ASC')));


        $selectedTab = "";
        if ($this->params['action'] == "mydashboard") {
            $selectedTab = "home";
        }elseif ($this->params['action'] == "myaccount") {
            $selectedTab = "home";
        } else {
            $allModules = $this->SystemModule->find("all", array('fields' => array('SystemModule.id, SystemModule.module, SystemModule.module_chinese, SystemModule.module_url, SystemModule.module_unique_name, SystemModule.group, SystemModule.order'), 'conditions' => array(), 'order' => array('SystemModule.order ASC')));

            $systemModuleUrl = "/" . $this->params['controller'] . "/" . $this->params['action'];

            foreach ($allModules as $moduleResult) {
                if ($systemModuleUrl == $moduleResult['SystemModule']['module_url']) {
                    $selectedTab = $moduleResult['SystemModule']['group'];
                }
            }
        }

        //$lang = $this->Cookie->read('lang');	Old code			

        $this->set('selectedTab', $selectedTab);
        $this->set('lang', $lang);
        $this->set('roleId', $roleId);
        $this->set('loggedInUSerRoleId', $roleId);

        if (!$roleId) {//No checks for superadmin
            $this->set('allowedMenusDetail', $allModulesDetails);
        } else {

            $moduleId = "";
            $i = 0;
            foreach ($allModulesDetails as $result) {
                if ($i) {
                    $moduleId .= "," . $result['SystemModule']['id'];
                } else {
                    $moduleId .= $result['SystemModule']['id'];
                }
                $i++;
            }

            App::Import("Model", "SystemModulePermission");
            $this->SystemModulePermission = new SystemModulePermission();
            //$allModulesDetails = $this->SystemModulePermission->find("all",array('conditions'=>array('SystemModulePermission.role_id'=>$roleId,'SystemModulePermission.system_module_id in ('.$moduleId.')','SystemModulePermission.permission'=>1), 'order' => array('SystemModule.order ASC')));
            //$this->set('allowedMenusDetail', $allModulesDetails);

            $allPermissionModulesDetails = $this->SystemModulePermission->find("all", array('conditions' => array('SystemModulePermission.role_id' => $roleId, 'SystemModulePermission.permission' => 1), 'order' => array('SystemModule.order ASC')));

            $allModulesDetails = array();

            $j = 0;
            $i = 0;
            
            $batchPaymentHoldURL = "/payrolls/hold_payment";
            $batchRevertPaymentHoldURL = "/payrolls/revert_hold_payment";
            $payableCheckingURLAdmin = "/reports/checkPayable";
            $mpfPaymentCalculationURLAdmin = "/payrolls/mpf_payment_calculation";
            $showLinkPayableCheckingURLAdmin = 0;
            $showBatchPaymentHoldURL = 0;
            $showBatchRevertPaymentHoldURL = 0;
            
            //Commenting below URL for MPF and Payment Calculation link for temporary
            //$showLinkMpfPaymentCalculationURLAdmin = 0;
            
            $createServiceUnitURL = "/serviceunits/add";
            $updateServiceUnitURL = "/serviceunits/edit";
            $showCreateServiceUnitURL = 0;
            $showUpdateServiceUnitURL = 0;

            $payeeprofileURL = "/payees/index";
            $payrolBatchURL = "/payrolls/index";
            $emailHistoryURL = "/payrolls/email_history";
            $emailHistoryDetailURL = "/payrolls/check_email_history";
            $showLinkPayeeprofileURL = 0;
            $showLinkPayrolBatchURL = 0;
            $showLinkEmailHistoryURL = 0;
            $showLinkEmailHistoryDetailURL = 0;
            
            $payeeprofileURLAdmin = "/payees/admin_index";
            $payrolBatchURLAdmin = "/payrolls/admin_index";
            $payrolBatchAdminVerificationURLAdmin = "/payrolls/admin_verification_index";
            $emailHistoryURLAdmin = "/payrolls/admin_email_history";
            $emailHistoryDetailURLAdmin = "/payrolls/admin_check_email_history";
            $showLinkPayeeprofileURLAdmin = 0;
            $showLinkPayrolBatchURLAdmin = 0;
            $showLinkPayrolBatchAdminVerificationURLAdmin = 0;
            $showLinkEmailHistoryURLAdmin = 0;
            $showLinkEmailHistoryDetailURLAdmin = 0;
            
            $serviceGroupAddURL = "/servicegroups/add";
            $serviceGroupEditURL = "/servicegroups/edit";
            $showServiceGroupAddURL = 0;
            $showServiceGroupEditURL = 0;

            $userPostAddURL = "/users/addpost";
            $userPostEditURL = "/users/editpost";
            $showUserPostAddURL = 0;
            $showUserPostEditURL = 0;
            
            $msgBoardAddURL = "/users/addmsgboard";
            $msgBoardEditURL = "/users/editmsgboard";
            $showMsgBoardAddURL = 0;
            $showMsgBoardEditURL = 0;

            $userRoleAddURL = "/users/addrole";
            $userRoleEditURL = "/users/editrole";
            $userPermissionsURL = "/users/showpermissions";
            $showUserRoleAddURL = 0;
            $showUserRoleEditURL = 0;
            $showPermissionsURL = 0;

            $userAddURL = "/users/createstaff";
            $userEditURL = "/users/updatestaff";
            $userDeleteURL = "/users/delete";
            $showUserAddURL = 0;
            $showUserEditURL = 0;
            $showUserDeleteURL = 0;

            $payeeAddURL = "/payees/add";
            $payeeEditURL = "/payees/edit";
            $payeeDeleteURL = "/payees/delete_payee";
            $showPayeeDeleteURL = 0;
            $showPayeeAddURL = 0;
            $showPayeeEditURL = 0;
            
            $payeeDeleteURLAdmin = "/payees/admin_delete_payee";
            $payeeEditURLAdmin = "/payees/admin_edit";
            $showPayeeDeleteURLAdmin = 0;
            $showPayeeEditURLAdmin = 0;
            
            $batchDeleteURL = "/payrolls/delete_batch";
            $batchDeleteURLAdmin = "/payrolls/admin_delete_batch";
            $showBatchDeleteURLAdmin = 0;
            $showBatchDeleteURL = 0;

            $batchPaymentRequestURL = "/payrolls/view";
            $uploadBatchExcelURL = "/payrolls/uploadbatch";
            $showBatchPaymentRequestURL = 0;
            $showUploadBatchExcelURL = 0;

            $batchDownloadPDFURL = "/payrolls/downloadpdf";
            $batchDownloadExcelURL = "/payrolls/download";
            $showBatchDownloadPDFURL = 0;
            $showBatchDownloadExcelURL = 0;

            $batchRequestEmailURL = "/payrolls/sendMail";
            $batchRequestDownloadPDFURL = "/payrolls/download_viewPDF";
            $batchRequestDownloadExcelURL = "/payrolls/download_view";
            $showBatchRequestDownloadPDFURL = 0;
            $showBatchRequestDownloadExcelURL = 0;
            $showBatchRequestEmailURL = 0;

            $batchRequestVerifyURL = "/payrolls/changeStatusVerify";
            $batchRequestApproveURL = "/payrolls/changeStatusApprove";
            $batchRequestEndorseURL = "/payrolls/changeStatusEndorse";
            $showBatchRequestEndorseURL = 0;
            $showBatchRequestVerifyURL = 0;
            $showBatchRequestApproveURL = 0;
            
            $batchPaymentRequestURLAdmin = "/payrolls/admin_view";
            $uploadBatchExcelURLAdmin = "/payrolls/admin_uploadbatch";
            $showBatchPaymentRequestURLAdmin = 0;
            $showUploadBatchExcelURLAdmin = 0;

            $batchDownloadPDFURLAdmin = "/payrolls/admin_downloadpdf";
            $batchDownloadExcelURLAdmin = "/payrolls/admin_download";
            $showBatchDownloadPDFURLAdmin = 0;
            $showBatchDownloadExcelURLAdmin = 0;

            $batchRequestDownloadPDFURLAdmin = "/payrolls/admin_download_viewPDF";
            $batchRequestDownloadExcelURLAdmin = "/payrolls/admin_download_view";
            $showBatchRequestDownloadPDFURLAdmin = 0;
            $showBatchRequestDownloadExcelURLAdmin = 0;

            $batchRequestVerifyURLAdmin = "/payrolls/admin_changeStatusVerify";
            $batchRequestApproveURLAdmin = "/payrolls/admin_changeStatusApprove";
            $showBatchRequestVerifyURLAdmin = 0;
            $showBatchRequestApproveURLAdmin = 0;
            
            $uploadBatchExcelURLAdminVerification = "/payrolls/admin_verification_uploadbatch";
            $batchDownloadPDFURLAdminVerification = "/payrolls/admin_verification_downloadpdf";
            $batchDownloadExcelURLAdminVerification = "/payrolls/admin_verification_download";
            $showBatchDownloadExcelURLAdminVerification = 0;
            $showBatchDownloadPDFURLAdminVerification = 0;
            $showUploadBatchExcelURLAdminVerification = 0;
            
            $batchPaymentRequestURLAdminVerification = "/payrolls/admin_verification_view";
            $showBatchPaymentRequestURLAdminVerification = 0;
            
            $batchRequestVerifyURLAdminVerification = "/payrolls/admin_verification_changeStatusVerify";
            $batchRequestApproveURLAdminVerification = "/payrolls/admin_verification_changeStatusApprove";
            $batchRequestCheckURLAdminVerification = "/payrolls/admin_verification_changeStatusCheck";
            $showBatchRequestVerifyURLAdminVerification = 0;
            $showBatchRequestApproveURLAdminVerification = 0;
            $showBatchRequestCheckURLAdminVerification = 0;
            
            $batchRequestDownloadPDFURLAdminVerification = "/payrolls/admin_verification_download_viewPDF";
            $batchRequestDownloadExcelURLAdminVerification = "/payrolls/admin_verification_download_view";
            $batchRequestEmailURLAdminVerification = "/payrolls/admin_verification_email";
            $batchRequestEditURLAdminVerification = "/payrolls/edit_payable";
            $showBatchRequestDownloadPDFURLAdminVerification = 0;
            $showBatchRequestDownloadExcelURLAdminVerification = 0;
            $showBatchRequestEmailURLAdminVerification = 0;
            $showBatchRequestEditURLAdminVerification = 0;
            
            $batchPaymentHoldURLAdminVerification = "/payrolls/admin_hold_payment";
            $batchRevertPaymentHoldURLAdminVerification = "/payrolls/admin_revert_hold_payment";
            $showBatchPaymentHoldURLAdminVerification = 0;
            $showBatchRevertPaymentHoldURLAdminVerification = 0;
            
            $contractAddURL = "/contracts/add";
            $contractEditURL = "/contracts/edit";
            $contractDeleteURL = "/contracts/delete";
            $showContractDeleteURL = 0;
            $showContractAddURL = 0;
            $showContractEditURL = 0;

            foreach ($allPermissionModulesDetails as $details) {
                if ($details['SystemModule']['module_type']) {

                    $allModulesDetails[$i]['SystemModulePermission']['id'] = $details['SystemModulePermission']['id'];
                    $allModulesDetails[$i]['SystemModulePermission']['system_module_id'] = $details['SystemModulePermission']['system_module_id'];
                    $allModulesDetails[$i]['SystemModulePermission']['role_id'] = $details['SystemModulePermission']['role_id'];
                    $allModulesDetails[$i]['SystemModulePermission']['permission'] = $details['SystemModulePermission']['permission'];
                    $allModulesDetails[$i]['SystemModulePermission']['staff_id'] = $details['SystemModulePermission']['staff_id'];
                    $allModulesDetails[$i]['SystemModulePermission']['modified'] = $details['SystemModulePermission']['modified'];
                    $allModulesDetails[$i]['SystemModulePermission']['created'] = $details['SystemModulePermission']['created'];


                    $allModulesDetails[$i]['SystemModule']['id'] = $details['SystemModule']['id'];
                    $allModulesDetails[$i]['SystemModule']['module'] = $details['SystemModule']['module'];
                    $allModulesDetails[$i]['SystemModule']['module_chinese'] = $details['SystemModule']['module_chinese'];
                    $allModulesDetails[$i]['SystemModule']['module_url'] = $details['SystemModule']['module_url'];
                    $allModulesDetails[$i]['SystemModule']['module_type'] = $details['SystemModule']['module_type'];
                    $allModulesDetails[$i]['SystemModule']['order'] = $details['SystemModule']['order'];
                    $allModulesDetails[$i]['SystemModule']['module_unique_name'] = $details['SystemModule']['module_unique_name'];
                    $allModulesDetails[$i]['SystemModule']['group'] = $details['SystemModule']['group'];
                    $allModulesDetails[$i]['SystemModule']['created'] = $details['SystemModule']['created'];
                    $allModulesDetails[$i]['SystemModule']['modified'] = $details['SystemModule']['modified'];
                    $i++;
                } else {
                    
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['id'] = $details['SystemModulePermission']['id'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['system_module_id'] = $details['SystemModulePermission']['system_module_id'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['role_id'] = $details['SystemModulePermission']['role_id'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['permission'] = $details['SystemModulePermission']['permission'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['staff_id'] = $details['SystemModulePermission']['staff_id'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['modified'] = $details['SystemModulePermission']['modified'];
                    $this->allSubModulesDetails[$j]['SystemModulePermission']['created'] = $details['SystemModulePermission']['created'];


                    $this->allSubModulesDetails[$j]['SystemModule']['id'] = $details['SystemModule']['id'];
                    $this->allSubModulesDetails[$j]['SystemModule']['module'] = $details['SystemModule']['module'];
                    $this->allSubModulesDetails[$j]['SystemModule']['module_chinese'] = $details['SystemModule']['module_chinese'];
                    $this->allSubModulesDetails[$j]['SystemModule']['module_url'] = $details['SystemModule']['module_url'];
                    $this->allSubModulesDetails[$j]['SystemModule']['module_type'] = $details['SystemModule']['module_type'];
                    $this->allSubModulesDetails[$j]['SystemModule']['order'] = $details['SystemModule']['order'];
                    $this->allSubModulesDetails[$j]['SystemModule']['module_unique_name'] = $details['SystemModule']['module_unique_name'];
                    $this->allSubModulesDetails[$j]['SystemModule']['group'] = $details['SystemModule']['group'];
                    $this->allSubModulesDetails[$j]['SystemModule']['created'] = $details['SystemModule']['created'];
                    $this->allSubModulesDetails[$j]['SystemModule']['modified'] = $details['SystemModule']['modified'];
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $contractAddURL && !$showContractAddURL) {
                        $showContractAddURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $contractEditURL && !$showContractEditURL) {
                        $showContractEditURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $contractDeleteURL && !$showContractDeleteURL) {
                        $showContractDeleteURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestEditURLAdminVerification && !$showBatchRequestEditURLAdminVerification) {
                        $showContractAddURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestEditURLAdminVerification && !$showBatchRequestEditURLAdminVerification) {
                        $showBatchRequestEditURLAdminVerification = 1;
                    }
                    
                    
                    //Commenting below URL for MPF and Payment Calculation link for temporary
                    /*
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $mpfPaymentCalculationURLAdmin && !$showLinkMpfPaymentCalculationURLAdmin) {
                        $showLinkMpfPaymentCalculationURLAdmin = 1;
                    }
                    */
                    
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payableCheckingURLAdmin && !$showLinkPayableCheckingURLAdmin) {
                        $showLinkPayableCheckingURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchPaymentHoldURL && !$showBatchPaymentHoldURL) {
                        $showBatchPaymentHoldURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRevertPaymentHoldURL && !$showBatchRevertPaymentHoldURL) {
                        $showBatchRevertPaymentHoldURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchPaymentHoldURLAdminVerification && !$showBatchPaymentHoldURLAdminVerification) {
                        $showBatchPaymentHoldURLAdminVerification = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRevertPaymentHoldURLAdminVerification && !$showBatchRevertPaymentHoldURLAdminVerification) {
                        $showBatchRevertPaymentHoldURLAdminVerification = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDeleteURLAdmin && !$showBatchDeleteURLAdmin) {
                        $showBatchDeleteURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDeleteURL && !$showBatchDeleteURL) {
                        $showBatchDeleteURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadPDFURLAdminVerification && !$showBatchRequestDownloadPDFURLAdminVerification) {
                        $showBatchRequestDownloadPDFURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadExcelURLAdminVerification && !$showBatchRequestDownloadExcelURLAdminVerification) {
                        $showBatchRequestDownloadExcelURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestEmailURLAdminVerification && !$showBatchRequestEmailURLAdminVerification) {
                        $showBatchRequestEmailURLAdminVerification = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestVerifyURLAdminVerification && !$showBatchRequestVerifyURLAdminVerification) {
                        $showBatchRequestVerifyURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestApproveURLAdminVerification && !$showBatchRequestApproveURLAdminVerification) {
                        $showBatchRequestApproveURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestCheckURLAdminVerification && !$showBatchRequestCheckURLAdminVerification) {
                        $showBatchRequestCheckURLAdminVerification = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchPaymentRequestURLAdminVerification && !$showBatchPaymentRequestURLAdminVerification) {
                        $showBatchPaymentRequestURLAdminVerification = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadPDFURLAdminVerification && !$showBatchDownloadPDFURLAdminVerification) {
                        $showBatchDownloadPDFURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadExcelURLAdminVerification && !$showBatchDownloadExcelURLAdminVerification) {
                        $showBatchDownloadExcelURLAdminVerification = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $uploadBatchExcelURLAdminVerification && !$showUploadBatchExcelURLAdminVerification) {
                        $showUploadBatchExcelURLAdminVerification = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestVerifyURL && !$showBatchRequestVerifyURL) {
                        $showBatchRequestVerifyURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestEndorseURL && !$showBatchRequestEndorseURL) {
                        $showBatchRequestEndorseURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestApproveURL && !$showBatchRequestApproveURL) {
                        $showBatchRequestApproveURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchPaymentRequestURL && !$showBatchPaymentRequestURL) {
                        $showBatchPaymentRequestURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $uploadBatchExcelURL && !$showUploadBatchExcelURL) {
                        $showUploadBatchExcelURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestEmailURL && !$showBatchRequestEmailURL) {
                        $showBatchRequestEmailURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadPDFURL && !$showBatchRequestDownloadPDFURL) {
                        $showBatchRequestDownloadPDFURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadExcelURL && !$showBatchRequestDownloadExcelURL) {
                        $showBatchRequestDownloadExcelURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadPDFURL && !$showBatchDownloadPDFURL) {
                        $showBatchDownloadPDFURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadExcelURL && !$showBatchDownloadExcelURL) {
                        $showBatchDownloadExcelURL = 1;
                    }
                    
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestVerifyURLAdmin && !$showBatchRequestVerifyURLAdmin) {
                        $showBatchRequestVerifyURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestApproveURLAdmin && !$showBatchRequestApproveURLAdmin) {
                        $showBatchRequestApproveURLAdmin = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchPaymentRequestURLAdmin && !$showBatchPaymentRequestURLAdmin) {
                        $showBatchPaymentRequestURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $uploadBatchExcelURLAdmin && !$showUploadBatchExcelURLAdmin) {
                        $showUploadBatchExcelURLAdmin = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadPDFURLAdmin && !$showBatchRequestDownloadPDFURLAdmin) {
                        $showBatchRequestDownloadPDFURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchRequestDownloadExcelURLAdmin && !$showBatchRequestDownloadExcelURLAdmin) {
                        $showBatchRequestDownloadExcelURLAdmin = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadPDFURLAdmin && !$showBatchDownloadPDFURLAdmin) {
                        $showBatchDownloadPDFURLAdmin = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $batchDownloadExcelURLAdmin && !$showBatchDownloadExcelURLAdmin) {
                        $showBatchDownloadExcelURLAdmin = 1;
                    }
                    

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $createServiceUnitURL && !$showCreateServiceUnitURL) {
                        $showCreateServiceUnitURL = 1;
                    }
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $updateServiceUnitURL && !$showUpdateServiceUnitURL) {
                        $showUpdateServiceUnitURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeprofileURL && !$showLinkPayeeprofileURL) {
                        $showLinkPayeeprofileURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payrolBatchURL && !$showLinkPayrolBatchURL) {
                        $showLinkPayrolBatchURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $emailHistoryURL && !$showLinkEmailHistoryURL) {
                        $showLinkEmailHistoryURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $emailHistoryDetailURL && !$showLinkEmailHistoryDetailURL) {
                        $showLinkEmailHistoryDetailURL = 1;
                    }
                    
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeprofileURLAdmin && !$showLinkPayeeprofileURLAdmin) {
                        $showLinkPayeeprofileURLAdmin = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payrolBatchURLAdmin && !$showLinkPayrolBatchURLAdmin) {
                        $showLinkPayrolBatchURLAdmin = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payrolBatchAdminVerificationURLAdmin && !$showLinkPayrolBatchAdminVerificationURLAdmin) {
                        $showLinkPayrolBatchAdminVerificationURLAdmin = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $emailHistoryURLAdmin && !$showLinkEmailHistoryURLAdmin) {
                        $showLinkEmailHistoryURLAdmin = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $emailHistoryDetailURLAdmin && !$showLinkEmailHistoryDetailURLAdmin) {
                        $showLinkEmailHistoryDetailURLAdmin = 1;
                    }
                    

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $serviceGroupAddURL && !$showServiceGroupAddURL) {
                        $showServiceGroupAddURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $serviceGroupEditURL && !$showServiceGroupEditURL) {
                        $showServiceGroupEditURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userPostAddURL && !$showUserPostAddURL) {
                        $showUserPostAddURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userPostEditURL && !$showUserPostEditURL) {
                        $showUserPostEditURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $msgBoardAddURL && !$showMsgBoardAddURL) {
                        $showMsgBoardAddURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $msgBoardEditURL && !$showMsgBoardEditURL) {
                        $showMsgBoardEditURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userRoleAddURL && !$showUserRoleAddURL) {
                        $showUserRoleAddURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userRoleEditURL && !$showUserRoleEditURL) {
                        $showUserRoleEditURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userPermissionsURL && !$showPermissionsURL) {
                        $showPermissionsURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userAddURL && !$showUserAddURL) {
                        $showUserAddURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userEditURL && !$showUserEditURL) {
                        $showUserEditURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $userDeleteURL && !$showUserDeleteURL) {
                        $showUserDeleteURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeAddURL && !$showPayeeAddURL) {
                        $showPayeeAddURL = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeEditURL && !$showPayeeEditURL) {
                        $showPayeeEditURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeDeleteURL && !$showPayeeDeleteURL) {
                        $showPayeeDeleteURL = 1;
                    }
                    
                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeDeleteURLAdmin && !$showPayeeDeleteURLAdmin) {
                        $showPayeeDeleteURLAdmin = 1;
                    }

                    if ($this->allSubModulesDetails[$j]['SystemModule']['module_url'] == $payeeEditURLAdmin && !$showPayeeEditURLAdmin) {
                        $showPayeeEditURLAdmin = 1;
                    }

                    $j++;
                }
            }
            $this->set('showContractEditURL', $showContractEditURL);
            $this->set('showContractDeleteURL', $showContractDeleteURL);
            $this->set('showContractAddURL', $showContractAddURL);
            
            $this->set('showMsgBoardAddURL', $showMsgBoardAddURL);
            $this->set('showMsgBoardEditURL', $showMsgBoardEditURL);
            
            $this->set('showBatchRevertPaymentHoldURLAdminVerification', $showBatchRevertPaymentHoldURLAdminVerification);
            $this->set('showBatchPaymentHoldURLAdminVerification', $showBatchPaymentHoldURLAdminVerification);
            $this->set('showBatchRevertPaymentHoldURL', $showBatchRevertPaymentHoldURL);
            $this->set('showBatchPaymentHoldURL', $showBatchPaymentHoldURL);
            $this->set('showBatchRequestEditURLAdminVerification', $showBatchRequestEditURLAdminVerification);
            
            //Commenting below URL for MPF and Payment Calculation link for temporary
            //$this->set('showLinkMpfPaymentCalculationURLAdmin', $showLinkMpfPaymentCalculationURLAdmin);
            
            $this->set('showLinkPayableCheckingURLAdmin', $showLinkPayableCheckingURLAdmin);
            $this->set('showBatchDeleteURL', $showBatchDeleteURL);
            $this->set('showBatchDeleteURLAdmin', $showBatchDeleteURLAdmin);
            $this->set('showPayeeDeleteURL', $showPayeeDeleteURL);
            $this->set('showUploadBatchExcelURLAdminVerification', $showUploadBatchExcelURLAdminVerification);
            $this->set('showBatchDownloadPDFURLAdminVerification', $showBatchDownloadPDFURLAdminVerification);
            $this->set('showBatchDownloadExcelURLAdminVerification', $showBatchDownloadExcelURLAdminVerification);
            $this->set('showBatchPaymentRequestURLAdminVerification', $showBatchPaymentRequestURLAdminVerification);
            $this->set('showBatchRequestVerifyURLAdminVerification', $showBatchRequestVerifyURLAdminVerification);
            $this->set('showBatchRequestApproveURLAdminVerification', $showBatchRequestApproveURLAdminVerification);
            $this->set('showBatchRequestCheckURLAdminVerification', $showBatchRequestCheckURLAdminVerification);
            $this->set('showBatchRequestDownloadPDFURLAdminVerification', $showBatchRequestDownloadPDFURLAdminVerification);
            $this->set('showBatchRequestDownloadExcelURLAdminVerification', $showBatchRequestDownloadExcelURLAdminVerification);
            $this->set('showBatchRequestEmailURLAdminVerification', $showBatchRequestEmailURLAdminVerification);
            
            $this->set('showLinkPayrolBatchAdminVerificationURLAdmin', $showLinkPayrolBatchAdminVerificationURLAdmin);

            $this->set('showBatchRequestVerifyURL', $showBatchRequestVerifyURL);
            $this->set('showBatchRequestEndorseURL', $showBatchRequestEndorseURL);
            $this->set('showBatchRequestApproveURL', $showBatchRequestApproveURL);

            $this->set('showBatchRequestEmailURL', $showBatchRequestEmailURL);
            $this->set('showBatchRequestDownloadPDFURL', $showBatchRequestDownloadPDFURL);
            $this->set('showBatchRequestDownloadExcelURL', $showBatchRequestDownloadExcelURL);

            $this->set('showBatchDownloadPDFURL', $showBatchDownloadPDFURL);
            $this->set('showBatchDownloadExcelURL', $showBatchDownloadExcelURL);

            $this->set('showBatchPaymentRequestURL', $showBatchPaymentRequestURL);
            $this->set('showUploadBatchExcelURL', $showUploadBatchExcelURL);
            
            $this->set('showBatchRequestVerifyURLAdmin', $showBatchRequestVerifyURLAdmin);
            $this->set('showBatchRequestApproveURLAdmin', $showBatchRequestApproveURLAdmin);

            $this->set('showBatchRequestDownloadPDFURLAdmin', $showBatchRequestDownloadPDFURLAdmin);
            $this->set('showBatchRequestDownloadExcelURLAdmin', $showBatchRequestDownloadExcelURLAdmin);

            $this->set('showBatchDownloadPDFURLAdmin', $showBatchDownloadPDFURLAdmin);
            $this->set('showBatchDownloadExcelURLAdmin', $showBatchDownloadExcelURLAdmin);

            $this->set('showBatchPaymentRequestURLAdmin', $showBatchPaymentRequestURLAdmin);
            $this->set('showUploadBatchExcelURLAdmin', $showUploadBatchExcelURLAdmin);

            $this->set('showPayeeAddURL', $showPayeeAddURL);
            $this->set('showPayeeEditURL', $showPayeeEditURL);
            
            $this->set('showPayeeDeleteURLAdmin', $showPayeeDeleteURLAdmin);
            $this->set('showPayeeEditURLAdmin', $showPayeeEditURLAdmin);

            $this->set('showUserAddURL', $showUserAddURL);
            $this->set('showUserEditURL', $showUserEditURL);
            $this->set('showUserDeleteURL', $showUserDeleteURL);

            $this->set('showUserRoleAddURL', $showUserRoleAddURL);
            $this->set('showUserRoleEditURL', $showUserRoleEditURL);
            $this->set('showPermissionsURL', $showPermissionsURL);

            $this->set('showUserPostAddURL', $showUserPostAddURL);
            $this->set('showUserPostEditURL', $showUserPostEditURL);

            $this->set('showCreateServiceUnitURL', $showCreateServiceUnitURL);
            $this->set('showUpdateServiceUnitURL', $showUpdateServiceUnitURL);

            $this->set('showLinkPayeeprofileURL', $showLinkPayeeprofileURL);
            $this->set('showLinkPayrolBatchURL', $showLinkPayrolBatchURL);
            $this->set('showLinkEmailHistoryURL', $showLinkEmailHistoryURL);
            $this->set('showLinkEmailHistoryDetailURL', $showLinkEmailHistoryDetailURL);
            
            $this->set('showLinkPayeeprofileURLAdmin', $showLinkPayeeprofileURLAdmin);
            $this->set('showLinkPayrolBatchURLAdmin', $showLinkPayrolBatchURLAdmin);
            $this->set('showLinkEmailHistoryURLAdmin', $showLinkEmailHistoryURLAdmin);
            $this->set('showLinkEmailHistoryDetailURLAdmin', $showLinkEmailHistoryDetailURLAdmin);

            $this->set('showServiceGroupAddURL', $showServiceGroupAddURL);
            $this->set('showServiceGroupEditURL', $showServiceGroupEditURL);

            $this->set('allSubModulesDetails', $this->allSubModulesDetails);
            $this->set('allowedMenusDetail', $allModulesDetails);
        }

        $mainTabsArr = array();
        $mainTabsLinkArr = array();
        $leftMenuStr = "";
        if (!isset($module)) {
            $module = "";
        }
        $view = new View($this);
        $html = $view->loadHelper('Html');

        foreach ($allModulesDetails as $result) {
            //To get all the menu for header based on any left menu

            if (!in_array($result['SystemModule']['group'], $mainTabsArr)) {
                $mainTabsArr[] = $result['SystemModule']['group'];
                $mainTabsLinkArr[$result['SystemModule']['group']]['order'] = $result['SystemModule']['order'];
                $mainTabsLinkArr[$result['SystemModule']['group']]['module_url'] = $result['SystemModule']['module_url'];
            }

            if ($mainTabsLinkArr[$result['SystemModule']['group']]['order'] >= $result['SystemModule']['order']) {
                $mainTabsLinkArr[$result['SystemModule']['group']]['order'] = $result['SystemModule']['order'];
                $mainTabsLinkArr[$result['SystemModule']['group']]['module_url'] = $result['SystemModule']['module_url'];
            }

            //To get all the menu for left panel
            $class = "";
            if ($result['SystemModule']['module_unique_name'] == $module) {
                $class = "active";
            }
            if ($result['SystemModule']['group'] == $selectedTab) {


                if ($lang != 'eng') {
                    $mainModuleName = $result['SystemModule']['module_chinese'];
                } else {
                    $mainModuleName = $result['SystemModule']['module'];
                }

                $leftMenuStr .= '<li>';
                $leftMenuStr .= $html->link($mainModuleName, array('controller' => '', 'action' => $result['SystemModule']['module_url']), array('class' => '' . $class . '', 'label' => false));
                $class = "";

                $leftMenuStr .= '</li>';
            }
        }

        $this->set('mainTabsArr', $mainTabsArr);
        $this->set('mainTabsLinkArr', $mainTabsLinkArr);
        $this->set('leftMenuStr', $leftMenuStr);
        /*         * ***** End Code added to show the left navigation************ */
    }

    function checkSystemAccess($moduleUrl = null) {

        $roleId = $this->Auth->User('role_id');

        if (!$roleId) {//No checks for superadmin
            return true;
        } else {

            if ($moduleUrl == '/payrolls/changeStatus' || $moduleUrl == '/payrolls/download_view' || $moduleUrl == '/payrolls/download' || $moduleUrl == '/payrolls/adminVerifierChangeStatus' || $moduleUrl == '/reports/download_report' || $moduleUrl == '/contracts/download' || $moduleUrl == '/reports/payment_history' || $moduleUrl == '/reports/payment_view' || $moduleUrl == '/reports/download' || $moduleUrl == '/payrolls/process_mail') {
                return true;
            }
            App::Import("Model", "SystemModule");
            $this->SystemModule = new SystemModule();
            $findModuleId = $this->SystemModule->find("first", array('fields' => array('SystemModule.id'), 'conditions' => array('SystemModule.module_url' => $moduleUrl)));

            if (is_array($findModuleId) && !empty($findModuleId)) {
                $systemModuleId = $findModuleId['SystemModule']['id'];

                App::Import("Model", "SystemModulePermission");
                $this->SystemModulePermission = new SystemModulePermission();
                $findDetail = $this->SystemModulePermission->find("count", array('conditions' => array('role_id' => $roleId, 'system_module_id' => $systemModuleId, 'permission' => 1)));

                if ($findDetail < 1) {
                    //$this->redirect('/errors/errors');
                    $msg = __("NO_PERMISSION", true);
                    $cls = "totalErrors";
                    $this->Session->setFlash('<div class=' . $cls . '>' . $msg . '</div>');
                    $this->redirect('/users/mydashboard');
                } else {
                    return true;
                }
            } else {
                $this->redirect('/');
            }
        }
    }
    
    /**
     * @method: proceedToLogin
     * @purpose: to proceed user to destroy existing and create New
     * @param none
     * @return void
     * */
    function proceedToLogin() {
        $logId = $this->Session->read('Auth.User.LogId');
        $userId = $this->Auth->User("id");
        if ($logId) {
            App::import('Model', 'Log');
            $this->Log = new Log();
            $logData = $this->Log->find('first', array(
                            'order' => array('Log.modified' => 'desc'),
                            'conditions' => array('Log.id' => $logId, 'Log.user_id' => $userId)
                        ));
            if (!empty($logData)) {
                $this->updateLogEntry();
            } else {
                $this->Session->setFlash(__('LOGGED IN BY OTHER WAY', true));
                $this->Session->destroy();
                $this->redirect($this->Auth->logout());
            }
        }
    }
    /**
     * @method: updateLogEntry
     * @purpose: update log entry for a user for further check
     * @param none
     * @return void
     * */
    function updateLogEntry() {
        $this->Log = new Log();
        $this->Log->create();
        $data = array();
        $data['Log']['id'] = $this->Auth->User("LogId");
        $data['Log']['modified'] = date('Y-m-d H:i:s');
        $this->Log->save($data);
    }
    
    /**
     * @method: checkLogin
     * @purpose: is logged in by other way
     * @param none
     * @return void
     * */
    public function checkLogin($email = null) {
        $userData = $this->User->find('first', array(
            'conditions' => array('User.email' => $email)
        ));
        
        if (!empty($userData['Log']['0'])) {
            $lastModified = strtotime($userData['Log'][0]['modified']);
            if ((time() - $lastModified) > Configure::read('Session.timeout')) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
    
    /**
     * @method: saveLogEntry
     * @purpose: create log entry for a user for further check
     * @param $userid
     * @return log id
     * */
    function saveLogEntry($userId = null) {
        $this->Log = new Log();
        $this->Log->create();
        $data = array();
        $data['Log']['user_id'] = $userId;
        $this->Log->save($data);
        return $this->Log->getInsertID();
    }
    
    /**
     * @method: saveActionLog
     * @purpose: create log entry for a user for further check
     * @param $userid
     * @return log id
     * */
    function saveActionLog($type = 'staff', $description = null) {
        $userId = $this->Auth->User("id");
        App::Import("Model", "UserLog");
        $this->UserLog = new UserLog();
        $userLogArr = array();
        $userLogArr['user_id'] = $userId;
        $userLogArr['type'] = $type;
        $userLogArr['description'] = $description;

        $this->UserLog->save($userLogArr);
    }
}
